/**
 * @author yadongzhao87
 * @date 2023-10-19
 * 清雷毫米波雷达相关接口
 */
const router = require('koa-router')();
const returnData=require("../util/returnstring");
const dotenv= require("dotenv");
dotenv.config({ path: `.env.${process.env.NODE_ENV}` })
const {
    InfluxDB,
    Point
} = require('@influxdata/influxdb-client');
// const { json } = require('@infra-node/sequelize');

const url = process.env.INFLUXDB_URL;
const token = process.env.INFLUXDB_TOKEN;

console.log("token:", process.env.INFLUXDB_TOKEN);

const client = new InfluxDB({
    url,
    token
})

// 定义路由前缀，没有可不添加
router.prefix('/api/qinglei')


// 获取清雷的生命体征信息回调
router.post('/smtzcallback', async (ctx, next) => {
    let callbackData = ctx.request.body;
    //设备在线则继续处理，不在线则不处理
    if (callbackData.isOnLine) {
        let org = `benyi`
        let bucket = `iot`
        let writeClient = client.getWriteApi(org, bucket, 'ns')
        let point = new Point('radar_shengming')
            .tag('isOnline', callbackData.isOnLine).tag("snCode", callbackData.snCode).tag("statusName",callbackData.statusName)
            .intField('breathBpm', callbackData.breathBpm).intField('heartBpm', callbackData.heartBpm).intField('inOutBed', callbackData.inOutBed).intField("outBedDuration",callbackData.outBedDuration).stringField("breathBpmRange",callbackData.breathBpmRange).stringField("heartBpmRange",callbackData.heartBpmRange).stringField("tooCloseFlag",callbackData.tooCloseFlag);
        writeClient.writePoint(point)
        writeClient.flush();
    }
    ctx.body=returnData(100000,[],"");
})
// 获取清雷的跌倒检测回调
router.post('/ddjccallback', async (ctx, next) => {
    let callbackData = ctx.request.body;
    //设备在线则继续处理，不在线则不处理
    if (callbackData.isOnLine) {
        let org = `benyi`
        let bucket = `iot`
        let writeClient = client.getWriteApi(org, bucket, 'ns')
        let point = new Point('radar_ddjc')
            .tag('isOnline', callbackData.isOnLine).tag("snCode", callbackData.snCode)
            .intField('exist', callbackData.exist).intField('fall', callbackData.fall);
        writeClient.writePoint(point)
        writeClient.flush();
    }
    ctx.body=returnData(100000,[],"");


})
// 获取清雷的实时报警回调
router.post('/ssbjcallback', async (ctx, next) => {
    let callbackData = ctx.request.body;
    //设备在线则继续处理，不在线则不处理
    if (callbackData) {
        let org = `benyi`
        let bucket = `iot`
        let alarmTime=new Date(callbackData.alarmTime);
        console.log(callbackData);
        alarmTimeTimestamp=alarmTime.getTime()/1000;
        let writeClient = client.getWriteApi(org, bucket, 'ns')
        let point = new Point('radar_ssbj').tag("snCode", callbackData.snCode)
            .intField('alarmTime', alarmTimeTimestamp).stringField('alarmType', callbackData.alarmType);
        writeClient.writePoint(point)
        writeClient.flush();
    }
    ctx.body=returnData(100000,[],"");


})
module.exports = router;